本文整理汇总了Python中aiofiles.open方法的典型用法代码示例。如果您正苦于以下问题:Python aiofiles.open方法的具体用法?Python aiofiles.open怎么用?Python aiofiles.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiofiles
的用法示例。
在下文中一共展示了aiofiles.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: File
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def File(node, filename='', json=True):
'''Open up a file and write lines to the file
Args:
node (Node): input stream
filename (str): filename to write
json (bool): load file line as json
'''
async def _file(data):
async with aiofiles.open(filename, mode='a') as fp:
if json:
fp.write(JSON.dumps(data))
else:
fp.write(data)
return data
ret = Node(foo=_file, name='File', inputs=1, graphvizshape=_OUTPUT_GRAPHVIZSHAPE)
node >> ret
return ret
示例2: asgi_send_file
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def asgi_send_file(
send, filepath, filename=None, content_type=None, chunk_size=4096
):
headers = {}
if filename:
headers["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)
first = True
async with aiofiles.open(str(filepath), mode="rb") as fp:
if first:
await asgi_start(
send,
200,
headers,
content_type or guess_type(str(filepath))[0] or "text/plain",
)
first = False
more_body = True
while more_body:
chunk = await fp.read(chunk_size)
more_body = len(chunk) == chunk_size
await send(
{"type": "http.response.body", "body": chunk, "more_body": more_body}
)
示例3: _download
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def _download(self, *, url: str, path: Path) -> None:
async with aiohttp_session(auth=self.auth) as session:
async with session.get(url) as response:
response.raise_for_status()
# download file
if aiofiles is not None:
async with aiofiles.open(str(path), mode='wb') as stream:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
await stream.write(chunk)
else:
with path.open(mode='wb') as stream:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
stream.write(chunk)
示例4: send_tweet_with_media
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def send_tweet_with_media():
# read the tweet's status
status = input("status: ")
path = ""
while not path and not os.path.exists(path):
path = input('file to upload:\n')
# read the most common input formats
path = urlparse(path).path.strip(" \"'")
async with aiofiles.open(path, 'rb') as media:
# optimize pictures if PIL is available
if PIL:
media = await process_media(media, path)
uploaded = await client.upload_media(media,
chunk_size=2**18,
chunked=True)
media_id = uploaded.media_id
await client.api.statuses.update.post(status=status,
media_ids=media_id)
示例5: download
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def download(self, session=None, chunk=-1):
if self.content:
if chunk < 0:
return self.content
else:
return self.content[:chunk]
Media.cache_dir.mkdir(exist_ok=True)
if self.cache.exists():
async with aiofiles.open(str(self.cache), mode='rb') as stream:
self.content = await stream.read()
if self.content_length == len(self.content):
return self.content
if session is None:
raise RuntimeError("No session")
async with session.get(self.url) as response:
print("downloading", self.filename)
self.content = await response.read()
async with aiofiles.open(str(self.cache), mode='wb') as stream:
await stream.write(self.content)
return self.content
示例6: run
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def run(self) -> bool:
"""Runs scanner"""
logger.info("Running pipeline with these steps:")
for n, filt in enumerate(self.filters):
logger.info(" %i. %s", n+1, filt.get_info())
res = super().run()
logger.info("")
logger.info("Recipe status statistics:")
for key, value in self.stats.most_common():
logger.info("%s: %s", key, value)
logger.info("SUM: %i", sum(self.stats.values()))
if self.status_fn:
with open(self.status_fn, "w") as out:
for rname, result in self.status:
out.write(f'{rname}\t{result.name}\n')
return res
示例7: apply
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def apply(self, recipe: Recipe) -> None:
if not recipe.is_modified():
raise self.NoChanges(recipe)
branch_name = self.branch_name(recipe)
changed = False
async with self.git.lock_working_dir:
self.git.prepare_branch(branch_name)
async with aiofiles.open(recipe.path, "w",
encoding="utf-8") as fdes:
await fdes.write(recipe.dump())
if recipe.version != recipe.orig.version:
msg = f"Update {recipe} to {recipe.version}"
elif recipe.build_number != recipe.orig.build_number:
msg = f"Bump {recipe} buildnumber"
else:
msg = f"Update {recipe}"
changed = self.git.commit_and_push_changes([recipe.path], branch_name, msg)
if changed:
# CircleCI appears to have problems picking up on our PRs. Let's wait
# a while before we create the PR, so the pushed branch has time to settle.
await asyncio.sleep(10) # let push settle before going on
elif not recipe.on_branch:
raise self.NoChanges(recipe)
示例8: main
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def main(self):
''''''
res = self.getAllapi();
if isinstance(res,dict):
print(res)
return
for item in res:
dt = {}
name = item.get('name',None) or item.get('treeName',None)
if name not in ['淘宝客API','淘宝搜索API','会员中心API','淘宝卡券平台','商户API','手淘分享','手机淘宝API']:
continue
for doc_item in item.get('catelogList',[]):
res = await self.getDetail(doc_item.get('docId',285),doc_item.get('docType',2))
dt.update(res)
# async with aiofiles.open(name += '.json','wb') as f:
# await f.write(json.dumps(dt))
with open(name + '.json','w') as f:
# f.write(json.dumps(dt))
json.dump(dt,f)
示例9: get_file_getter
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def get_file_getter(file_path,
file_size,
size_limit=1024*64):
# NB: if the file size is small, we read its bytes and return them;
# otherwise, a lazy reader is returned; that returns the file in chunks
if file_size > size_limit:
async def file_chunker():
async for chunk in get_file_chunks(file_path, size_limit):
yield chunk
yield b''
return file_chunker
async def file_getter():
async with aiofiles.open(file_path, mode='rb') as file:
data = await file.read()
yield data
yield b''
return file_getter
示例10: _read_from_persisted
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def _read_from_persisted() -> Optional[List[dict]]:
try:
async with aiofiles.open(_last_check_file()) as open_file:
last_check = json.loads(await open_file.read())
if _is_recent(last_check):
return last_check["data"]
else:
return None
except json.JSONDecodeError as e:
logging.warning("Unable to decode persisted releases check: %s", str(e))
return None
except FileNotFoundError:
return None
示例11: fetch_player
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def fetch_player(self, tag=None, auth=None, **kwargs):
"""Fetch player"""
url = 'https://api.rushstats.com/v1/player/{}'.format(clean_tag(tag))
fn = os.path.join(CACHE_PLAYER_PATH, "{}.json".format(tag))
try:
data = await self.fetch(url=url, auth=auth)
except APIServerError:
if os.path.exists(fn):
async with aiofiles.open(fn, mode='r') as f:
data = json.load(await f.read())
else:
raise
else:
async with aiofiles.open(fn, mode='w') as f:
json.dump(data, f)
return RWPlayer(data)
示例12: fetch_team
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def fetch_team(self, tag=None, auth=None, **kwargs):
"""Fetch player"""
url = 'https://api.rushstats.com/v1/team/{}'.format(clean_tag(tag))
fn = os.path.join(CACHE_TEAM_PATH, "{}.json".format(tag))
try:
data = await self.fetch(url=url, auth=auth)
except APIServerError:
if os.path.exists(fn):
async with aiofiles.open(fn, mode='r') as f:
data = json.load(await f.read())
else:
raise
else:
async with aiofiles.open(fn, mode='w') as f:
json.dump(data, f)
return RWTeam(data)
示例13: _rwset_config
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def _rwset_config(self, ctx):
"""Team config"""
if len(ctx.message.attachments) == 0:
await self.bot.say(
"Please attach config yaml with this command. "
"See config.example.yml for how to format it."
)
return
attach = ctx.message.attachments[0]
url = attach["url"]
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
with open(TEAM_CONFIG_YML, "wb") as f:
f.write(await resp.read())
await self.bot.say(
"Attachment received and saved as {}".format(TEAM_CONFIG_YML))
self.settings['config'] = TEAM_CONFIG_YML
dataIO.save_json(JSON, self.settings)
await self.bot.delete_message(ctx.message)
示例14: api_fetch_player
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def api_fetch_player(tag=None, auth=None, session=None, **kwargs):
"""Fetch player"""
url = 'https://api.brawlstars.com/v1/players/%23{}'.format(clean_tag(tag))
fn = os.path.join(CACHE_PLAYER_PATH, "{}.json".format(tag))
try:
data = await api_fetch(url=url, auth=auth, session=session)
except APIServerError:
if os.path.exists(fn):
async with aiofiles.open(fn, mode='r') as f:
data = json.load(await f.read())
else:
raise
else:
async with aiofiles.open(fn, mode='w') as f:
json.dump(data, f)
return BSPlayer(data)
示例15: _bsset_config
# 需要导入模块: import aiofiles [as 别名]
# 或者: from aiofiles import open [as 别名]
def _bsset_config(self, ctx):
"""Band config"""
if len(ctx.message.attachments) == 0:
await self.bot.say(
"Please attach config yaml with this command. "
"See config.example.yml for how to format it."
)
return
attach = ctx.message.attachments[0]
url = attach["url"]
async with self.session.get(url) as resp:
with open(BAND_CONFIG_YML, "wb") as f:
f.write(await resp.read())
await self.bot.say(
"Attachment received and saved as {}".format(BAND_CONFIG_YML))
self.settings['config'] = BAND_CONFIG_YML
dataIO.save_json(JSON, self.settings)
await self.bot.delete_message(ctx.message)