class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
with open('file','r') as f:
text = f.read()
git merge --abort
Сreating games
import asyncio
async def async_io():
print("async_io start")
result = 100
print("async_io end")
return result
async def async_db_query(data):
print("async_db_query start")
result = await asyncio.sleep(1, result=500)
print("async_db_query end")
return result
async def async_calculating(data):
print("async_calculating start")
res = 0
for i in range(1000):
res += 1
print("async_calculating end")
return res
async def task():
print("Task start")
res_async = await async_io()
res = await asyncio.gather(
async_db_query(res_async),
async_calculating(res_async)
)
task_calc = sum(res)
print(f"Task end. Result: {task_calc}")
return task_calc
loop = asyncio.get_event_loop()
loop.create_task(task())
loop.run_forever()
package main
import (
"encoding/json"
"fmt"
)
type TemplateCategory struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Template struct {
Name string `json:"name"`
CategoryInfoRaw *json.RawMessage `json:"category_info"`
CategoryInfo *TemplateCategory
}
func (t *Template) UnmarshalJSON(data []byte) error {
type template Template
if err := json.Unmarshal(data, (*template)(t)); err != nil {
return err
}
if t.CategoryInfoRaw == nil || string(*t.CategoryInfoRaw) == "[]" {
t.CategoryInfo = nil
} else {
if err := json.Unmarshal(*t.CategoryInfoRaw, &t.CategoryInfo); err != nil {
return err
}
}
return nil
}
func main() {
res := &Template{}
str1 := `{
"name": "Мой шаблон",
"category_info": {
"id": 109,
"name": "Тест"
}
}`
str2 := `{
"name": "Мой шаблон",
"category_info": []
}`
_ = json.Unmarshal([]byte(str1), res)
fmt.Printf("%+v\n", res)
fmt.Printf("%+v\n", res.CategoryInfo)
_ = json.Unmarshal([]byte(str2), res)
fmt.Printf("%+v", res)
}