python-3.6

How to create a new table in a MySQL DB from a pandas dataframe

谁都会走 提交于 2019-12-06 15:53:46
I recently transitioned from using SQLite for most of my data storage and management needs to MySQL. I think I've finally gotten the correct libraries installed to work with Python 3.6, but now I am having trouble creating a new table from a dataframe in the MySQL database. Here are the libraries I import: import pandas as pd import mysql.connector from sqlalchemy import create_engine In my code, I first create a dataframe from a CSV file (no issues here). def csv_to_df(infile): return pd.read_csv(infile) Then I establish a connection to the MySQL database using this def function: def mysql

PyQt MainWindow using multiprocessing on Windows

跟風遠走 提交于 2019-12-06 14:51:18
I try to create a PyQt application. In order to run process in background and keep the PyQt5 application available for new instruction, I want to use multiprocessing . On the Windows OS, when I call a function from the Qt MainWindow class with multiprocessing.process , I have an error about pickling this class. But it is running find on Linux. Here is an example: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import multiprocessing # from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class Frame_main(QMainWindow): def __init__(self, parent = None):

Memory-efficient large dataset streaming to S3

送分小仙女□ 提交于 2019-12-06 10:35:28
问题 I am trying to copy over S3 large dataset (larger than RAM) using SQL alchemy. My constraints are: I need to use sqlalchemy I need to keep memory pressure at lowest I don't want to use the local filsystem as intermediary step to send data to s3 I just want to pipe data from a DB to S3 in a memory efficient way. I can to do it normal with data sets (using below logic) but with larger dataset I hit a buffer issue. The first problem I solved is that executing a query usually buffers the result

IllegalArgumentException with Spark collect() on Jupyter

房东的猫 提交于 2019-12-06 05:51:45
问题 I have a setup with Jupyter 4.3.0, Python 3.6.3 (Anaconda), and PySpark 2.2.1. The following example will fail when run through Jupyter: sc = SparkContext.getOrCreate() rdd = sc.parallelize(['A','B','C']) rdd.collect() Below is the complete stack trace: --------------------------------------------------------------------------- Py4JJavaError Traceback (most recent call last) <ipython-input-35-0d4a2ca9edf4> in <module>() 2 3 rdd = sc.parallelize(['A','B','C']) ----> 4 rdd.collect() /usr/local

How to bind async method to a keystroke in Tkinter?

六眼飞鱼酱① 提交于 2019-12-06 04:46:24
Consider the following example: import asyncio import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() self.create_widgets() self._configure_bindings() # I believe it is not possible # to do this if the method needs # to be async as well def create_widgets(self): pass def _configure_bindings(self): self.bind('<F5>', self.spam) # what's the proper way? # does this method need to be async as well? async def spam(self, event): await self.do_something() async def do_something(): pass async def run_tk(root): try: while True: root.update() await asyncio.sleep(.01) except tk

Bad lock optimization in asyncio

狂风中的少年 提交于 2019-12-06 03:53:29
Update: Edited title to focus on the main problem. See my answer for full update. In the following code, a() and b() are identical. Each of them counts from 0 to 9 concurrently while acquiring and yielding a lock every 2 counts. import asyncio lock = asyncio.Lock() def a (): yield from lock.acquire() for i in range(10): print('a: ' + str(i)) if i % 2 == 0: lock.release() yield from lock.acquire() lock.release() def b (): yield from lock.acquire() for i in range(10): print('b: ' + str(i)) if i % 2 == 0: lock.release() yield from lock.acquire() lock.release() asyncio.get_event_loop().run_until

Why isn't __instancecheck__ being called?

这一生的挚爱 提交于 2019-12-06 03:37:20
问题 I have the following python3 code: class BaseTypeClass(type): def __new__(cls, name, bases, namespace, **kwd): result = type.__new__(cls, name, bases, namespace) print("creating class '{}'".format(name)) return result def __instancecheck__(self, other): print("doing instance check") print(self) print(other) return False class A(metaclass=BaseTypeClass): pass print(type(A)) print(isinstance(A(), A)) and when I run it on Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit

No module named '__main__.demo'; '__main__' is not a package python3

浪子不回头ぞ 提交于 2019-12-06 02:44:29
问题 If I execute main.py it works fine, the problem is when I execute demo2.py |myPackage |subPackage demo.py demo2.py main.py main.py from ludikDriver.demo2 import demo2_print demo2_print() demo2.py from .demo import demoprint def demo2_print(): print("demo2") demoprint() demo2_print() demo.py def demoprint(): print("demo") Error: from .demo import demoprint ModuleNotFoundError: No module named '__main__.demo'; '__main__' is not a package Should I have __init__.py ? 回答1: If you drop the . , it

Read text from clipboard in Windows using ctypes

被刻印的时光 ゝ 提交于 2019-12-06 01:45:56
I'm trying to get the text stored in the clipboard by just using ctypes in Python 3.6 . I tested a lot of solutions I found on Stack and GitHub, but they only work for Python 2 to Python 3.4 . This is the code you'll find almost everywhere: from ctypes import * def get_clipboard_text(): text = "" if windll.user32.OpenClipboard(c_int(0)): h_clip_mem = windll.user32.GetClipboardData(1) windll.kernel32.GlobalLock.restype = c_char_p text = windll.kernel32.GlobalLock(c_int(h_clip_mem)) windll.kernel32.GlobalUnlock(c_int(h_clip_mem)) windll.user32.CloseClipboard() return text I tested it in Python 3

Rounding to specific numbers in Python 3.6

瘦欲@ 提交于 2019-12-05 17:56:10
I'm trying to make a dive table that has some numbers that aren't in a pattern that I can see so I have to manually add all the values, but I need to grab the input and round it to the nearest number in the dictionary. I'll need to convert the input back to string for the output to be correct: CODE: class DepthTable: def __init__(self): self.d35 = {"10": "A", "19": "B", "25": "C", "29": "D", "32": "E", "36": "F", } def getpressureGroup(self, depth, time): if depth == "35": output = self.d35[time] else: output = "No info for that depth" print(output) if __name__ == "__main__": depthtable =