几个月前我注册了一个个人 GitLab 运行器,但我不再使用它。如何完全删除它,使其不会显示在我的 GitLab CI/CD 设置页面上?
tf.set_random_seed(SEED)
没影响,我可以告诉......例如,在IPython笔记本中多次运行下面的代码每次都会产生不同的输出:
import tensorflow as tf
tf.set_random_seed(42)
sess = tf.InteractiveSession()
a = tf.constant([1, 2, 3, 4, 5])
tf.initialize_all_variables().run()
a_shuf = tf.random_shuffle(a)
print(a.eval())
print(a_shuf.eval())
sess.close()
Run Code Online (Sandbox Code Playgroud)
如果我明确设置种子:a_shuf = tf.random_shuffle(a, seed=42)
,每次运行后输出都是相同的.但是,如果我已经打电话,为什么我需要设置种子tf.set_random_seed(42)
?
使用numpy的等效代码正常工作:
import numpy as np
np.random.seed(42)
a = [1,2,3,4,5]
np.random.shuffle(a)
print(a)
Run Code Online (Sandbox Code Playgroud) 我正在创建一个django webserver,允许用户在本地机器上运行一些"可执行文件"并通过网页分析它们的输出.
我之前使用过Celery任务队列,以便在类似的情况下运行"可执行文件".但是,在阅读了Python concurrent.futures之后,我开始怀疑我是否应该使用ThreadPoolExecutor
,或者ProcessPoolExecutor
(或者ThreadPoolExecutor
在a ProcessPoolExecutor
:D中)?
谷歌搜索我只能找到一个相关的问题比较芹菜和龙卷风,它转向单独使用龙卷风.
我应该使用Celery还是PoolExecutor
我的简单网络服务器,为什么?
我有一个Pandas DataFrame(称为df
),我想将其上传到MySql数据库。数据框具有列[ A,B,C ],数据库中的表具有列[ ID,A,B和C ]。数据库中的ID列是自动递增的主键。
我可以使用df.to_sql('table_name', engine)
命令将数据帧上传到数据库。但是,这没有给我任何有关数据库分配给传入数据的ID列的值的信息。我获得此信息的唯一方法是使用A,B,C列的值查询数据库:
select
ID, A, B, C
from db_table
where (A, B, C) in ((x1, y1, z1), (x2, y2, z2), ...)
Run Code Online (Sandbox Code Playgroud)
但是,当我插入大量数据时,此查询将花费很长时间。
有没有更简单快捷的方法来获取数据库分配给传入数据的ID列的值?
编辑1: 我可以根据下面的user3364098的回答自己分配ID列。但是,我的工作是并行运行的管道的一部分。如果我自己分配ID列,则有可能将相同的ID值分配给同时上传的不同数据框。这就是为什么我想将ID分配任务委托给数据库。
解决方案: 我最终自己分配了ID列,并在上载数据时对表进行了锁定,以确保没有其他进程上载具有相同id值的数据。基本上:
try:
engine.execute('lock tables `table_name` write')
max_id_query = 'select max(ID) FROM `table_name`' …
Run Code Online (Sandbox Code Playgroud) 我正在使用seabornpairplot来绘制几个自变量与因变量的关系图。
import seaborn as sns
iris = sns.load_dataset("iris")
x_vars = ['sepal_length', 'sepal_width', 'petal_length']
y_vars = ['petal_width']
pp = sns.pairplot(data=iris, x_vars=x_vars, y_vars=y_vars)
Run Code Online (Sandbox Code Playgroud)
现在我想向第二个和第三个子图添加 y 轴刻度和标签。
添加 y 轴标签很简单:
pp.set(ylabel='petal_width')
Run Code Online (Sandbox Code Playgroud)
但我一生都无法弄清楚如何显示 y_ticklabels。
像:
pp.set(yticklabels=np.arange(-0.5, 3.01, 0.5))
Run Code Online (Sandbox Code Playgroud)
或者:
for i in range(3):
ax = pp.axes[0,i]
ax.set_yticks(np.arange(-0.5, 3.01, 0.5))
ax.set_yticklabels(np.arange(-0.5, 3.01, 0.5))
ax.set_visible(True)
Run Code Online (Sandbox Code Playgroud)
没有什么区别。
如何使用pandas 执行完全外连接 两个数据帧的交叉连接而没有共同的列?
在MySQL中,您可以简单地执行:
SELECT *
FROM table_1
[CROSS] JOIN table_2;
Run Code Online (Sandbox Code Playgroud)
但在熊猫中,做:
df_1.merge(df_2, how='outer')
Run Code Online (Sandbox Code Playgroud)
给出错误:
MergeError: No common columns to perform merge on
Run Code Online (Sandbox Code Playgroud)
我到目前为止最好的解决方案是使用sqlite
:
import sqlalchemy as sa engine = sa.create_engine('sqlite:///tmp.db') df_1.to_sql('df_1', engine) df_2.to_sql('df_2', engine) df = pd.read_sql_query('SELECT * FROM df_1 JOIN df_2', engine)
说我有以下抽象类Foo
:
import abc
class Foo(abc.ABC):
@abc.abstractmethod
def bar(self):
raise NotImplementedError
Run Code Online (Sandbox Code Playgroud)
我该怎么把这个bar
方法放在身体里?
我看到了很多代码raise NotImplementedError
,如上所示.但是,这似乎是多余的,因为任何未实现的子类bar
都会引发TypeError: Can't instantiate abstract class Foo with abstract methods bar
它实例化的时间.
将Pythonic bar
留空是否如下:
import abc
class Foo(abc.ABC):
@abc.abstractmethod
def bar(self):
...
Run Code Online (Sandbox Code Playgroud)
这是在抽象基类的Python文档中所做的,但我不确定这只是占位符还是如何编写代码的实际示例.
如果bar
只留下三个点(...
),我应该何时使用NotImplementedError
?
我收到一个 MyPy 错误“缺少返回语句”,即使我检查了函数内的所有可能情况。
例如,在下面的代码中,MyPy 仍然给我一个错误"9: error: Missing return statement"
,即使color
只能是Color.RED
, Color.GREEN
, or Color.BLUE
,我测试了所有这些情况!
class Color(enum.IntEnum):
RED: int = 1
GREEN: int = 2
BLUE: int = 3
def test_enum(color: Color) -> str:
if color == Color.RED:
return "red"
elif color == Color.GREEN:
return "green"
elif color == Color.BLUE:
return "blue"
Run Code Online (Sandbox Code Playgroud) 我正在使用aiohttp
.
一些协程需要使用库来调用aiomysql
数据库。的文档aiomysql
有以下示例:
import asyncio
import aiomysql
loop = asyncio.get_event_loop()
async def test_example():
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='', db='mysql',
loop=loop)
cur = await conn.cursor()
await cur.execute("SELECT Host,User FROM user")
print(cur.description)
r = await cur.fetchall()
print(r)
await cur.close()
conn.close()
loop.run_until_complete(test_example())
Run Code Online (Sandbox Code Playgroud)
我的问题是关于全局变量的定义loop
:
loop = asyncio.get_event_loop()
Run Code Online (Sandbox Code Playgroud)
我真的需要将其loop
作为全局变量保存在某个地方,还是可以asyncio.get_event_loop()
在需要时调用它?
例如,在上面的代码示例中,当我连接到数据库时,我可以获得事件循环:
conn = await aiomysql.connect(host='127.0.0.1', port=3306,
user='root', password='', db='mysql',
loop=asyncio.get_event_loop())
Run Code Online (Sandbox Code Playgroud)
调用是否会产生不小的运行时成本asyncio.get_event_loop()
或我遗漏的其他内容?
我有一个 python 文件,它使用 SQLAlchemy 来定义给定数据库中的所有表,包括所有适用的索引和外键约束。该文件如下所示:
Base = declarative_base()
class FirstLevel(Base):
__tablename__ = 'first_level'
first_level_id = Column(Integer, index=True, nullable=False, primary_key=True, autoincrement=True)
first_level_col1 = Column(String(100), index=True)
first_level_col2 = Column(String(100))
first_level_col3 = Column(String(100))
class SecondLevel(Base):
__tablename__ = 'second_level'
second_level_id = Column(Integer, index=True, nullable=False, primary_key=True, autoincrement=True)
first_level_id = Column(None, ForeignKey(FirstLevel.first_level_id, onupdate='cascade', ondelete='cascade', deferrable=True), index=True, nullable=False)
second_level_col1 = Column(String(100), index=True)
second_level_col2 = Column(String(100))
second_level_col3 = Column(String(100))
class ThirdLevel(Base):
__tablename__ = 'third_level'
third_level_id = Column(Integer, index=True, nullable=False, primary_key=True, autoincrement=True)
first_level_id = Column(None, ForeignKey(FirstLevel.first_level_id, onupdate='cascade', ondelete='cascade', deferrable=True), …
Run Code Online (Sandbox Code Playgroud) python ×9
pandas ×3
sqlalchemy ×2
aio-mysql ×1
aiohttp ×1
celery ×1
database ×1
django ×1
gitlab-ci ×1
matplotlib ×1
mypy ×1
mysql ×1
postgresql ×1
python-3.x ×1
seaborn ×1
tensorflow ×1
type-hinting ×1