我试图在Python中解析基本的iso格式化日期时间字符串,但我很难做到这一点.请考虑以下示例:
>>> import json
>>> from datetime import datetime, date
>>> import dateutil.parser
>>> date_handler = lambda obj: obj.isoformat()
>>> the_date = json.dumps(datetime.now(), default=date_handler)
>>> print the_date
"2017-02-18T22:14:09.915727"
>>> print dateutil.parser.parse(the_date)
Traceback (most recent call last):
File "<input>", line 1, in <module>
print dateutil.parser.parse(the_date)
File "/usr/local/lib/python2.7/site-packages/dateutil/parser.py", line 1168, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python2.7/site-packages/dateutil/parser.py", line 559, in parse
raise ValueError("Unknown string format")
ValueError: Unknown string format
Run Code Online (Sandbox Code Playgroud)
我也尝试使用常规strptime解析它:
>>> print datetime.strptime(the_date, '%Y-%m-%dT%H:%M:%S')
# removed rest of the error output
ValueError: …
Run Code Online (Sandbox Code Playgroud) 我有一个 Django 项目,其中有一个我想测试的函数。简化后,该函数如下所示:
class InvalidUrlError(Exception):
pass
def get_info_from_url(url):
try:
return url.split(':')[1].split('/')[0]
except Exception:
raise InvalidUrlError(f"Invalid url: {url}")
Run Code Online (Sandbox Code Playgroud)
我的测试如下所示:
class ToolsTestCase(TestCase):
def test_get_info_from_url_wrong_formatted_url(self):
self.assertRaises(InvalidUrlError, get_info_from_url("https://acc.images.data.ourcompany.com/"))
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到以下输出:
$ ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
....E
======================================================================
ERROR: test_get_info_from_url_wrong_formatted_url (checker.tests.ToolsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kramer65/repos/auth-proxy/app/checker/tools.py", line 10, in get_info_from_url
return url.split(':')[1].split('/')[0]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last): …
Run Code Online (Sandbox Code Playgroud) 我开始使用Go,我现在正在编写一个简单的程序,它从传感器中读取数据并将其放入一个通道中进行一些计算.我现在的工作如下:
package main
import (
"fmt"
"time"
"strconv"
)
func get_sensor_data(c chan float64) {
time.Sleep(1 * time.Second) // wait a second before sensor data starts pooring in
c <- 2.1 // Sensor data starts being generated
c <- 2.2
c <- 2.3
c <- 2.4
c <- 2.5
}
func main() {
s := 1.1
c := make(chan float64)
go get_sensor_data(c)
for {
select {
case s = <-c:
fmt.Println("the next value of s from the channel: " + strconv.FormatFloat(s, …
Run Code Online (Sandbox Code Playgroud) 我是新手,所以请保持温柔。
因此,我已经在某些代码中使用互斥锁了几周了。我了解其背后的概念:锁定对特定资源的访问权限,与之交互(读或写),然后为其他人再次解锁。
我使用的互斥锁代码主要是复制粘贴调整。该代码可以运行,但是我仍在努力解决它的内部问题。到目前为止,我一直在结构中使用互斥锁来锁定该结构。今天,我发现了这个示例,这使我完全不清楚互斥锁实际上是在锁定什么。下面是一段示例代码:
var state = make(map[int]int)
var mutex = &sync.Mutex{}
var readOps uint64
var writeOps uint64
// Here we start 100 goroutines to execute repeated reads against the state, once per millisecond in each goroutine.
for r := 0; r < 100; r++ {
go func() {
total := 0
for {
key := rand.Intn(5)
mutex.Lock()
total += state[key]
mutex.Unlock()
atomic.AddUint64(&readOps, 1)
time.Sleep(time.Millisecond)
}
}()
}
Run Code Online (Sandbox Code Playgroud)
这里让我感到困惑的是,互斥锁和应该锁定的值之间似乎没有任何联系。直到今天,我还认为互斥锁可以锁定特定的变量,但是看这段代码,似乎可以以某种方式将整个程序锁定为仅执行锁定下方的行,直到再次运行解锁为止。我想这意味着所有其他goroutine都会暂停片刻,直到再次运行解锁。由于代码已编译,因此我想它可以知道在lock()
和之间访问了哪些变量unlock()
,但是我不确定是否是这种情况。
如果所有其他程序都暂停片刻,这听起来不像是真正的多处理程序,那么我想我对所发生的事情没有很好的了解。
有人可以帮助我了解计算机如何知道应锁定哪些变量吗?