首页 \ 问答 \ 在Python中构建API调用(Structuring API Calls in Python)

在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
更新时间:2022-02-06 06:02

最满意答案

你的编译器在/ 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.

相关问答

更多
  • find
  • 安装包“libgl1-mesa-dev”: sudo apt-get install libgl1-mesa-dev You should install package "libgl1-mesa-dev": sudo apt install libgl1-mesa-dev
  • 你的编译器在/ 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, ...
  • 标志-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 ...
  • 由于这是链接器错误,因此您可能遇到以下两个问题之一: 你没有安装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 ...
  • 尝试: 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 ...
  • 自从我看到了这个: 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)