def get_adj_dic(orgcode): gantry_adj = pd.read_csv(ws + f'/data/{orgcode}_gantry_hard_lon_lat.csv') gantry_adj = gantry_adj[['o_name', 'd_name']].values.tolist() adj_dic = {} for row in gantry_adj: if row[0] in adj_dic.keys(): adj_dic[row[0]].append(row[1]) else: adj_dic[row[0]] = [row[1]] return adj_dic
时间: 2024-04-06 17:28:58 浏览: 49
这段代码实现了一个函数get_adj_dic,其输入参数是orgcode,输出结果是一个邻接表形式的字典adj_dic。具体操作是:首先读取一个名为orgcode_gantry_hard_lon_lat.csv的csv文件,该文件有两列,分别为o_name和d_name,表示起点和终点的名称;然后将o_name和d_name转换为一个列表,即gantry_adj;接着遍历gantry_adj列表,将其中的元素按照起点进行分组,得到邻接表adj_dic,其中每个起点对应的终点为一个列表。最后返回adj_dic。
相关问题
early.warning.url=https://10.80.248.40:8180/api/v1/getRealtimeAlarm early.warning.request.body={"orgCode":1201}如何放入application.yml中
早期警告URL(`early.warning.url`)和请求体(`early.warning.request.body`)可以在Spring Boot应用的配置文件`application.yml`中设置为环境变量,以便于管理和复用。这里是一个示例:
```yaml
# application.yml
# 设置早期警告服务地址
spring:
application:
config:
properties:
earlyWarningUrl: https://10.80.248.40:8180/api/v1/getRealtimeAlarm
# 设置请求体内容作为JSON
restTemplate:
requestConverters:
default:
charset: UTF-8
body:
json:
enabled: true
Interceptors:
default:
applyTo: HEAD, GET, POST, PUT, DELETE, PATCH
headers:
Content-Type: 'application/json'
requestBody:
expressions:
- '{ "orgCode": 1201 }' # 使用表达式的方式设置动态请求体
```
在这个例子中,`spring.application.config.properties.earlyWarningUrl`设置了环境变量早起警告URL,而`restTemplate.requestBody.expressions`用于动态地构建请求体。`orgCode`字段值在这里是硬编码为1201,如果需要从外部(如数据库或配置中心)获取,可以修改成对应的表达式。
//获取用户id String userId = UserAgent.get().getUserId(); //根据id拿到用户组织 Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid(); String orgCode = userMapByUid.get(userId).getOrgCode(); //所有的行权限 列权限 List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); List<SchemaRowRule> userRowRuleList = new ArrayList<>(); DataPermissionSchema build = DataPermissionSchema.builder().build(); //设置行权限 根据用户id和用户组织去获取 for (SchemaRowRuleEo schemaRowRuleEo : schemaRowRuleList) { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); if(userlist.contains(userId)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } } } build.setSchemaRowRuleList(userRowRuleList);代码优化及原因
可以将循环中的if语句提取出来,避免重复代码。同时,使用stream流简化代码,并提高代码可读性。代码优化后如下:
```
String userId = UserAgent.get().getUserId();
Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid();
String orgCode = userMapByUid.get(userId).getOrgCode();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userId) || orgList.contains(orgCode);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = SchemaRowRule.builder().build();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
DataPermissionSchema build = DataPermissionSchema.builder().build();
build.setSchemaRowRuleList(userRowRuleList);
```
优化后的代码更加简洁易读,使用stream流可以避免重复代码,同时也可以提高代码的执行效率。
阅读全文