我需要使用密码登录服务,我需要在Python中做一些事情,但是我不想将密码存储在代码中。
我做了一些研究,发现base64编码。这看起来不错,但是无论如何都需要将密码存储在代码中。
>>> import base64
>>> encoded = base64.b64encode('data to be encoded') # password stored in the code
>>> encoded
'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
'data to be encoded'
Run Code Online (Sandbox Code Playgroud)
对于已经使用Python进行密码管理的人们,最佳选择是什么?将密码存储在文件中?仅使用b64encode()生成的字符串并将其解码,程序需要密码吗?
我试图将一个元素扩展到Python中的列表,但是,它不是在索引'i'中扩展字符串,而是扩展索引'i'中字符串的每个字符.
例如,我有一个名为'strings'的列表,只有一个字符串'string1'和一个名为'final_list'的空列表.
我想将'strings'的第一个元素扩展到'final_list',所以我这样做final_list.extend(strings[0]).但是,对应于插入的字符串,而不是以"长度为1"结尾的"final_list",列表最终的长度为7.
如果有帮助,这是我的代码:
con = connect()
i = 0
new_files = []
while i < len(files):
info_file = obter_info(con, files[i])
if info_file [5] == 0: #file not processed
new_files.append(files[i])
i += 1
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能让它工作?
我想编写一个算法,删除字符串中由大写字符开头的每个单词.
例如:
原字符串:"今天是星期五29点."
期望的结果:"是29Th."
我写了这个算法,但它不完整:
def removeUpperCaseChars(str: String) = {
for (i <- 0 to str.length - 1) {
if (str.charAt(i).isUpper) {
var j = i
var cont = i
while (str.charAt(j) != " ") {
cont += 1
}
val subStr = str.substring(0, i) + str.substring(cont, str.length - 1)
println(subStr)
}
}
}
Run Code Online (Sandbox Code Playgroud)
它(据说)用大写字符删除每个单词而不是仅删除以大写字符开头的单词.更糟糕的是,Scala没有给出任何结果.
任何人都可以帮我解决这个问题吗?
有没有办法将 if 放在 Scala 的火柴盒中?
像这样的东西:
def createSchedules(listTask: List[TaskOrder], physicalResources: List[Physical], humanResources: List[Human], previousTime: Duration): List[TaskSchedule] = listTask match {
case TaskOrder(id, time, physicalRes, order) :: t =>
val taskScheduleHumanResources = setHumanResources(physicalRes, humanResources)
if (physicalRes != null)
new TaskSchedule(
order,
order.getProduct(),
Task(id, time, physicalRes),
physicalRes,
taskScheduleHumanResources,
order.quantity,
previousTime,
previousTime + time) :: createSchedules(t, physicalRes, humanResources, previousTime + time)
case Nil => Nil
}
Run Code Online (Sandbox Code Playgroud)
这样做我得到一个错误说:
类型不匹配; 找到:所需单位:列表[Objects.TaskSchedule]
最好的方法是什么?
我从 Scala 开始,在功能上很难思考。
我有以下变量:
val dateTimeBegin = Calendar.getInstance()
dateTimeBegin.set(2016, Calendar.MAY, 25, 12, 0, 0)
val dateTimeEnd = Calendar.getInstance()
dateTimeEnd.set(2016, Calendar.MAY, 25, 16, 0, 0)
var slotsString = List[String]()
var slots = List[Calendar]()
Run Code Online (Sandbox Code Playgroud)
然后,我写了这个while循环:
while (dateTimeBegin.getTime().compareTo(dateTimeEnd.getTime()) == -1) {
slotsString = dateTimeBegin.getTime().toString() :: slotsString
dateTimeBegin.add(Calendar.MINUTE, 50)
}
Run Code Online (Sandbox Code Playgroud)
它基本上选择 dateTimeBegin 和 dateTimeEnd 并创建 50 分钟的插槽。
这是 while 循环给出的输出:
2016年5月25日星期三12:00:00 WEST 2016 WEST 25 12:50:00 WEST 2016 WEST 25 13:40:00 WEST 2016 WEST 2016 WEST 14:30:00 WEST 2016 WEST 202006 WEST
但是,我知道这不是在 Scala …