LCEL语法管道操作符“|”实现原理

管道符号“|”的含义是“或者”,Python解析器在解析时a | b语句时,如果发现两个对象之间存在“|”符号,会先尝试让对象b调用a的__or__方法。所以a | b 等价于 a.__or__(b)。

LangChain通过实现Runable类并重写__or__方法内容,来实现LCEL语法。

以下用一段代码来实现原理

基本原理实现


# 定义一个Runable类
class Runable:
    def __init__(self,func):
        self.func = func
    # 重写__or__方法
    def __or__(self,other):
        def chained_func(*args,**kwargs):
            # # self是a,other是b,这里相当于把a方法的返回结果作为入参传递给了b
            return other(self.func(*args,**kwargs))
        return Runable(chained_func)
    def __call__(self, *args, **kwargs):
        return self.func(*args, **kwargs)

# 定义一个x*2的func
def double(x):
    return x * 2

# 定义一个x+1的func
def add_one(x):
    return x + 1

def format_output(x):
    return ">>> {x} <<<".format(x=x)

# 使用Runable对以上函数进行包装
add_one = Runable(add_one)
double = Runable(double)


# 使用Runable对象的__or__方法
# chain = add_one.
### Java 实现根据经纬度计算日出和日落时间 以下是基于太阳位置算法的一个简单实现,用于通过给定的经纬度以及日期来计算当天的日出和日落时间。此方法依赖于天文计算中的标准公式[^1]。 ```java import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class SunriseSunsetCalculator { private static final double PI = Math.PI; private static final double DEG_TO_RAD = PI / 180.0; public static void main(String[] args) { double latitude = 40.7128; // 替换为实际纬度 double longitude = -74.0060; // 替换为实际经度 LocalDate date = LocalDate.now(); // 当前日期 ZonedDateTime sunriseTime = calculateSunrise(date, latitude, longitude); ZonedDateTime sunsetTime = calculateSunset(date, latitude, longitude); System.out.println("Sunrise Time: " + sunriseTime.format(DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println("Sunset Time: " + sunsetTime.format(DateTimeFormatter.ISO_LOCAL_TIME)); } private static ZonedDateTime calculateSunrise(LocalDate date, double latitude, double longitude) { return computeSunEventTime(date, latitude, longitude, true); } private static ZonedDateTime calculateSunset(LocalDate date, double latitude, double longitude) { return computeSunEventTime(date, latitude, longitude, false); } private static ZonedDateTime computeSunEventTime(LocalDate date, double latitude, double longitude, boolean isRiseTime) { int dayOfYear = date.getDayOfYear(); ZoneId zoneId = ZoneId.systemDefault(); double lngHour = longitude / 15.0; double t = dayOfYear + ((6 - lngHour) / 24); // 日序数调整 double meanAnomaly = (0.9856 * t) - 3.289; double sunLongitude = meanAnomalyToSunLongitude(meanAnomaly); double rightAscension = sunLongitudeToRightAscension(sunLongitude); double sinDec = Math.sin(sunLongitude * DEG_TO_RAD) * Math.sin(23.44 * DEG_TO_RAD); double cosH = (Math.cos(-0.83 * DEG_TO_RAD) - (sinDec * Math.sin(latitude * DEG_TO_RAD))) / (Math.cos(sinDec) * Math.cos(latitude * DEG_TO_RAD)); double h = Math.acos(cosH) / DEG_TO_RAD; h = isRiseTime ? 360 - h : h; double localMeanNoon = (720 - (4 * longitude) - (lngHour * 60)) + (h * 4); double utcTime = localMeanNoon / 60; return ZonedDateTime.of(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), (int) utcTime, (int) ((utcTime % 1) * 60), 0, 0, zoneId); } private static double meanAnomalyToSunLongitude(double m) { double l = m + (1.916 * Math.sin(m * DEG_TO_RAD)) + (0.020 * Math.sin(2 * m * DEG_TO_RAD)) + 282.634; while (l >= 360) l -= 360; while (l < 0) l += 360; return l; } private static double sunLongitudeToRightAscension(double l) { double ra = Math.atan(0.91764 * Math.tan(l * DEG_TO_RAD)) / DEG_TO_RAD; if (ra < 0) ra += 360; return ra; } } ``` #### 关键说明 - **输入参数**:该程序接受三个主要参数——`latitude`(纬度)、`longitude`(经度)和 `date`(日期)。这些数据决定了具体的地理位置目标日期。 - **核心逻辑**:利用天文学上的均差修正法,结合地球自转角度变化规律,推导出特定地的日出/日落时刻[^2]。 - **时区处理**:最终返回的时间会自动转换成运行环境所在的本地时区。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值