在Python中构建API调用(Structuring API Calls in Python)
我很难确定用于构造数据的最佳实践,以调用包含比特币价格的各种API。 我希望能够调用多个API,而无需重复代码。
我最初的想法是为我要调用的每个API构建类,并将它们的属性(api_id,url和json_tree(我想从中提取数据的json路径)提供给BtcAPI类,然后将它们吐出来。
*注意,在读取BtcAPI工作之前,Coindesk / Bitstamp类尚未与该类交互。 在我遇到麻烦之前,我想问一下我应该怎么做... *
现在,我想知道我是否应该制作它们,例如:
coindesk = ['http://www.something.com', 'coindesk', '["time"]["updated"]']
......并且只是迭代它们中的每一个。 或者说,或者其他各种各样的东西。 这里指出了什么数据结构?
我基本上是在寻找一些代码审查(因为这段代码不起作用,我不想将它发送到代码审查堆栈)并了解最佳实践:告诉我你认为我在哪里搞砸可怕的,我能做些什么来更好地构建这些数据? 我是一个蟒蛇和oop noob。 我可以在程序上做到这一点,但这将是丑陋和多余的。 我想我的课程也有点错误。 见解? 帮帮我? 谢谢!
谢谢!
import json import urllib.request #The BtcAPI class works well when you feed it static variables. It returns json. class BtcAPI: def __init__(self, url, api_id): self.url = url self.api_id = api_id def btc_api_call(self): hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' } req = urllib.request.Request(url, headers=hdr) readdata = urllib.request.urlopen(req) json_data = readdata.read() json_dict = json.loads(json_data) return(json_dict) class Coindesk: api_id = 'Coindesk' url = 'https://api.coindesk.com/v1/bpi/currentprice.json' json_tree = json_dict['time']['updated'] def __init__(self): self.current_us_price = current_us_price class Bitstamp: api_id = 'Bitstamp' url = 'https://www.bitstamp.net/api/ticker/' json_tree = json_dict['last'] def __init__(self): self.current_us_price = current_us_price coindesk_url = Coindesk() coindeskoutput = coindesk_url.url print(coindeskoutput)
I'm having difficulty in determining what the best practices are for structuring data for calling various APIs that contain bitcoin prices. I want to be able to call multiple APIs, without duplicate code.
My initial thought was to build classes for each of the APIs I would be calling, and feed their attributes (api_id, url, and json_tree (json path I'd like to pull data from) to the BtcAPI class, and spit them out.
*Note, before reading that while BtcAPI works, the Coindesk/Bitstamp classes do not yet interact with that class. I want to ask about the way I should do this before I go to the trouble...*
Now, I'm wondering if I shouldn't make them lists, like:
coindesk = ['http://www.something.com', 'coindesk', '["time"]["updated"]']
...and just iterate through each of them. Or dicts, or any variety of other thing. What data structure is indicated here?
I am basically looking for a bit of a code review (since this code isn't working, I don't want to send it to the code review stack) and an understanding of best practices: tell me where you think I'm screwing up horribly, and what I can do to structure this data better? I'm a python and oop noob. I could do this procedurally, but it would be ugly and superfluous. I imagine I'm using classes a bit wrong, too. Insights? Help? Thank you!
Thank you!
import json import urllib.request #The BtcAPI class works well when you feed it static variables. It returns json. class BtcAPI: def __init__(self, url, api_id): self.url = url self.api_id = api_id def btc_api_call(self): hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' } req = urllib.request.Request(url, headers=hdr) readdata = urllib.request.urlopen(req) json_data = readdata.read() json_dict = json.loads(json_data) return(json_dict) class Coindesk: api_id = 'Coindesk' url = 'https://api.coindesk.com/v1/bpi/currentprice.json' json_tree = json_dict['time']['updated'] def __init__(self): self.current_us_price = current_us_price class Bitstamp: api_id = 'Bitstamp' url = 'https://www.bitstamp.net/api/ticker/' json_tree = json_dict['last'] def __init__(self): self.current_us_price = current_us_price coindesk_url = Coindesk() coindeskoutput = coindesk_url.url print(coindeskoutput)
原文:https://stackoverflow.com/questions/48572494
最满意答案
你的编译器在/ usr / lib中寻找一个名为libGL.so的lib,它是/usr/lib/mesa/libGL.so,Mesa的libGL的符号链接。 您还有来自nVidia驱动程序的libGL(可能是275.28版本,请参阅libGL名称:libGL.so.275.28)。 修改符号链接指向nVidia的符号链接使编译器不再是Mesa的编译器,而是nVidia的编译器。
Your compiler was looking for a lib called libGL.so in /usr/lib, which was a symbolic link to /usr/lib/mesa/libGL.so, Mesa's libGL. You also have the libGL from your nVidia drivers (which are probably in 275.28 version, see the libGL name : libGL.so.275.28). Modifying the symlink to point to nVidia's one gives your compiler no longer the Mesa's one, but the nVidia's one.
相关问答
更多-
LINUX 如何查看JPG文件[2022-06-13]
find -
Qt:找不到-lGL错误(Qt: can't find -lGL error)[2022-12-30]
安装包“libgl1-mesa-dev”: sudo apt-get install libgl1-mesa-dev You should install package "libgl1-mesa-dev": sudo apt install libgl1-mesa-dev -
找到lGL(cannont find lGL)[2023-02-10]
你的编译器在/ usr / lib中寻找一个名为libGL.so的lib,它是/usr/lib/mesa/libGL.so,Mesa的libGL的符号链接。 您还有来自nVidia驱动程序的libGL(可能是275.28版本,请参阅libGL名称:libGL.so.275.28)。 修改符号链接指向nVidia的符号链接使编译器不再是Mesa的编译器,而是nVidia的编译器。 Your compiler was looking for a lib called libGL.so in /usr/lib, ... -
ld.exe找不到-lGL(ld.exe cannot find -lGL)[2023-12-15]
标志-lGL是链接到opengl库,但在Windows上,opengl库称为opengl32 ,而不是libgl。 所以你不应该使用-lGL ,如果你已经在opengl32中进行了连接,那么这就是你所需要的。 The flag -lGL is to link to the opengl library, but on Windows the opengl library is called opengl32, not libgl. So you shouldn't use -lGL and if you'r ... -
Qt debian / ubuntu:无法编译,错误:找不到-lGL(Qt debian/ubuntu: Can't compile, error: cannot find -lGL)[2023-08-17]
由于这是链接器错误,因此您可能遇到以下两个问题之一: 你没有安装libGL libGL已安装,但不在系统路径中。 如果libGL没有安装,你可以安装它: sudo apt-get install libgl1-mesa-dev 我认为是正确的方案。 我没有一个方便的debian机器,所以我无法测试它。 如果您安装了该软件包,则需要将其添加到系统路径中。 您需要将其附加到环境变量LD_LIBRARY_PATH或创建位于/etc/ld.so.conf.d/的.conf文件。 再次,我没有一台debian机器来验 ... -
GL包根据您的图形卡而有所不同。 安装正确的版本: fglrx fglrx-updates nvidia-173 nvidia-304-updates nvidia-304 nvidia-331-updates 安装它们: sudo apt-get install
另外,确保你安装了依赖关系。 sudo apt-get install cmake g++ gdb git libsdl2-dev zlib1g-dev The GL package is different ... -
构建Qt应用程序时,“/ usr / bin / ld:找不到-lGL”(“/usr/bin/ld: cannot find -lGL” when building a Qt application)[2023-08-23]
尝试: apt-get install libgl1-mesa-glx 开发包实际上不是问题。 您有链接器错误,而不是编译器错误。 根据您的图形卡,您可以使用专门的OpenGL实现,例如libgl1-nvidia-glx和NVIDA卡。 Try: apt-get install libgl1-mesa-glx The dev packages are actually not the problem. You have a linker error, not a compiler error. Depend ... -
qt5:找不到-lGL(qt5: cannot find -lGL)[2023-05-18]
自从我看到了这个: Cannot start '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory 然后解决方案可以在这里找到: cmake无法正常工作,无法执行qmake 。 Since I have seen this: Cannot start '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory then the solution ... -
安装devtoolsets-6软件包并确保从shell中启动qtcreator,在那里你已经使用了enobeled devtoolsets 6.然后你将拥有一个现代的开发环境。 还要确保已安装NVidia驱动程序(或AMD等效驱动程序)以获得正确的OpenGL支持。 Install the devtoolsets-6 package and make sure you start qtcreator from a shell where you have enabeled devtoolsets 6. Th ...
-
根据您是要链接32位还是64位可执行文件,需要/usr/lib32/nvidia-352/libGL.so.352.63或/usr/lib/nvidia-352/libGL.so.352.63。 当链接器找不到-lGL时,它正在寻找libGL.a或libGL.so .. 此外,您只需要使用-L指定目录,例如-L/usr/lib/nvidia-352 。 最后,即使这样你可以链接,在运行时仍然找不到库,除非你在运行可执行文件之前设置LD_LIBRARY_PATH,或者你添加-Wl,-rpath,/usr/li ...