AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-7169895

David Frick's questions

Martin Hope
David Frick
Asked: 2023-09-03 23:24:00 +0800 CST

在 Pyside6 中单击按钮时协程不会运行

  • 5

我正在尝试运行一个协程,该协程通过单击按钮获取数据。它用于aiohttp获取数据。该按钮装饰有@asyncSlot()fromqasync但是,当我单击该按钮时,没有任何反应,甚至没有打印语句。我认为 asyncio 示例与我的用例不匹配。我尝试在 asyncio 运行时移动,看看是否有帮助。我有点困惑,因为我没有遇到任何错误。为什么我的函数没有运行?

我的代码:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.container = QWidget()  # Controls container widget.
        self.controlsLayout = QVBoxLayout()

        # -- Search bar and settings button
        self.search_layout = QHBoxLayout()
        self.search_bar = QLineEdit()
        self.search_button = QPushButton("Search")     
        self.search_button.clicked.connect(self.update_data)  # search by entered text
        self.search_layout.addWidget(self.search_button)
        self.controlsLayout.addLayout(self.search_layout)

        # Our tab container with adjustable number of tabs
        self.tab_container = TabContainer()
        # Add out tabs widget
        self.controlsLayout.addWidget(self.tab_container)
        self.container.setLayout(self.controlsLayout)
        self.setCentralWidget(self.container

    @asyncSlot()
    async def update_data(self):
        stock_tickers = self.search_bar.text()

        print("Entered tickers: ", stock_tickers)

        short_interest = asyncio.create_task(self.setup_data(stock))
        short_interest = await short_interest
        print('SHORT INTEREST:', short_interest)

        # Make a table in a new tab here
        # ..............................


    async def setup_data(self, stock):
        short_interest = await get_data(stock)   # this function is our main coroutine
        # process the data here ...... as previous method return json


async def main():
    app = QApplication(sys.argv)
    loop = qasync.QEventLoop(app)
    asyncio.set_event_loop(loop)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    asyncio.run(main())

其中 my get_data(stock),我们可以将其视为基本的 aiohttp json 调用,定义为

async def get_data(stock_ticker):
    async with aiohttp.ClientSession() as session:
        async with session.get('https://www.finra.org/finra-data/browse-catalog/equity-short-interest/data') as response:
            cfruid = session.cookie_jar.filter_cookies('https://www.finra.org/')["__cfruid"].value
            print("CFRUID", cfruid)
            five_months_date = date.today() + relativedelta(months=-5)

            headers = {
                'authority': 'services-dynarep.ddwa.finra.org',
                'accept': 'application/json, text/plain, */*',
                'accept-language': 'en-US,en;q=0.6',
                'content-type': 'application/json',
                'cookie': f'XSRF-TOKEN={cfruid};',
                'origin': 'https://www.finra.org',
                'referer': 'https://www.finra.org/',
                'x-xsrf-token': cfruid,
            }

            json_data = {
                'fields': [
                    'settlementDate',
                    'issueName',
                    'symbolCode',
                    'marketClassCode',
                    'currentShortPositionQuantity',
                    'previousShortPositionQuantity',
                    'changePreviousNumber',
                    'changePercent',
                    'averageDailyVolumeQuantity',
                    'daysToCoverQuantity',
                    'revisionFlag',
                ],
                'dateRangeFilters': [],
                'domainFilters': [],
                'compareFilters': [
                    {
                        'fieldName': 'symbolCode',
                        'fieldValue': 'GME',
                        'compareType': 'EQUAL',
                    },
                    {
                        'fieldName': 'settlementDate',
                        'fieldValue': str(five_months_date),
                        'compareType': 'GREATER',
                    },
                ],
                'multiFieldMatchFilters': [],
                'orFilters': [],
                'aggregationFilter': None,
                'sortFields': [
                    '-settlementDate',
                    '+issueName',
                ],
                'limit': 50,
                'offset': 0,
                'delimiter': None,
                'quoteValues': False,
            }

            async with session.post('https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/group/OTCMarket/name/ConsolidatedShortInterest',
                              headers=headers, json=json_data) as response2:
                short_interest_data = await response2.json()
                return short_interest_data

通过创建任务并等待它,我能够让协程工作并在自己的文件中返回数据。我也尝试使用loop替代。我缺少什么?请注意,其他常规方法在单击按钮时起作用,因此我知道这只是 asyncio 拒绝运行。

编辑:它似乎挂在aiohttp未运行我的打印语句的部分的开头get_data

python
  • 1 个回答
  • 32 Views
Martin Hope
David Frick
Asked: 2023-08-29 01:30:16 +0800 CST

为什么我对 JSONObject 的键的第二次迭代没有运行?

  • 5

我有一个JSONObject我正在用来创建一个表。但是,当我第二次使用这些键创建每个单元格时,它什么也没做。我添加了日志语句来测试这一点。不key打印单数 s。叫我难住了。

class MovieFragment : Fragment(), FinraDataInterface {

    private lateinit var binding: MovieFragmentBinding

    val finraDataInterface: FinraDataInterface = this

    override fun drawTable(jsonArrayData: JSONArray) {
//         ------ Draw the table -----
//         Draw the keys first
        val headerRow = TableRow(requireActivity())
        Log.d("jsonArray", jsonArrayData[0].toString())
        val firstRecord = jsonArrayData[0] as JSONObject
        val keys = firstRecord.keys()

        for (key in keys) {
            val textView = TextView(requireActivity())
            textView.text = key
            headerRow.addView(textView)
        }
        binding.table.addView(headerRow)

        // Draw the data second
        for (i in 0 until jsonArrayData.length()) {
            val record = jsonArrayData[i] as JSONObject
            Log.d("Record", record.toString())
            val tableRow = TableRow(requireActivity())
            Log.d("Table Row", "Table Row created")
            Log.d("Keys", listOf(keys).toString())

            // Error is right here.
            for (key in keys) {
                Log.d("key", key)
                val textView = TextView(requireActivity())
                val cellText = record[key] as String
                textView.text = cellText
                tableRow.addView(textView)
            }
            binding.table.addView(tableRow)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = MovieFragmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        finraGetBondInfo("AMC")
    }




    private fun finraGetBondInfo(companyName: String) {
        val url = "https://www.finra.org/finra-data/fixed-income/corp-and-agency"

        val queue = Volley.newRequestQueue(requireActivity())
        var cfruid = ""

        val stringRequest = object: StringRequest(Request.Method.GET, url,
            { response ->
                Log.d("A", "Response is: " + response.substring(0,500))
            },
            { error ->
                Log.e("Error", error.toString())
            })
        {
            override fun  parseNetworkResponse(response : NetworkResponse) : Response<String> {
                // since we don't know which of the two underlying network vehicles
                // will Volley use, we have to handle and store session cookies manually
                Log.i("response", response.headers.toString());
                val cookies = HttpCookie.parse(response.headers?.get("Set-Cookie"))
                Log.d("Cookies:", cookies[0].toString())
                val indexOfEqualSign = cookies[0].toString().indexOf('=')
                cfruid = cookies[0].toString().substring(indexOfEqualSign + 1)
                Log.d("Cfruid:", cfruid)


                return super.parseNetworkResponse(response)
            }
        }

        queue.add(stringRequest)

        // Create our JSON Object for the finra call
        val json = JSONObject()
        val fieldsArray = JSONArray()
        fieldsArray.put("issueSymbolIdentifier")
        fieldsArray.put("issuerName")
        fieldsArray.put("isCallable")
        fieldsArray.put("productSubTypeCode")
        fieldsArray.put("couponRate")
        fieldsArray.put("maturityDate")
        fieldsArray.put("industryGroup")
        fieldsArray.put("moodysRating")
        fieldsArray.put("standardAndPoorsRating")
        fieldsArray.put("lastSalePrice")
        fieldsArray.put("lastSaleYield")
        json.put("fields", fieldsArray)

        json.put("dateRangeFilters", JSONArray())
        json.put("domainFilters", JSONArray())
        json.put("compareFilters", JSONArray())

        val multiJson = JSONObject()
        multiJson.put("fuzzy", false)
        multiJson.put("searchValue", companyName)
        multiJson.put("synonym", true)
        val subfieldsJson = JSONObject()
        subfieldsJson.put("name", "issuerName")
        subfieldsJson.put("boost", 1)
        val fieldsArray2 = JSONArray()
        fieldsArray2.put(subfieldsJson)
        multiJson.put("fields", fieldsArray2)
        val multiArray = JSONArray()
        multiArray.put(multiJson)

        json.put("multiFieldMatchFilters", multiArray)
        json.put("orFilters", JSONArray())
        json.put("aggregationFilter", JSONObject.NULL)
        val sortFieldsArray = JSONArray()
        sortFieldsArray.put("+issuerName")
        json.put("sortFields", sortFieldsArray)
        json.put("limit", 50)
        json.put("offset", 0)
        json.put("delimiter", JSONObject.NULL)
        json.put("quoteValues", false)

        Log.d("JSON", json.toString())


        val url2 = "https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/group/FixedIncomeMarket/name/CorporateAndAgencySecurities"
        val request2 = object: JsonObjectRequest(Request.Method.POST, url2, json,
            { response ->
                // TODO replace this with our calls to make tab2 display bond data
                val returnBody =  response["returnBody"] as JSONObject
                var stringData = returnBody["data"] as String
                stringData = stringData.replace("\\n", "").replace("\\", "")
                val jsonArrayData = JSONArray(stringData)

                finraDataInterface.drawTable(jsonArrayData)
            },
            { error ->
                Log.e("Error", error.toString())
            })
        {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = HashMap<String, String>()
                headers["Authority"] = "services-dynarep.ddwa.finra.org"
                headers["Accept"] = "application/json, text/plain, */*"
                headers["Cookie"] = "XSRF-TOKEN=$cfruid;"
                headers["Origin"] = "https://www.finra.org"
                headers["Referer"] = "https://www.finra.org/"
                headers["X-XSRF-token"] = cfruid
                headers["user-agent"] =  "python-requests/2.31.0"
                Log.d("headers:", headers.toString())
                return headers
            }

            override fun getBodyContentType(): String {
                return "application/json"
            }
        }
        queue.add(request2)

    }

}

让我特别困惑的是,使用键创建标题行的第一个 for 循环工作正常。我尝试将内容转换为不同的类型,看看是否存在某种类型错误。然而,这只是第二个 for 循环没有运行。我没有收到任何错误消息。我缺少什么?谢谢。

android
  • 1 个回答
  • 31 Views
Martin Hope
David Frick
Asked: 2023-08-27 08:29:11 +0800 CST

Kotlin - 如何正确并行处理数据?

  • 5

我正在抓取一个网站以获取信息。片段包含两个检索数​​据然后绘制数据的方法。我必须异步处理数据以防止主线程被锁定。但是,当我运行代码时,很明显异步数据处理发生无序,这导致某些索引超出范围。我不确定该怎么办。我试图限制并行性,但没有帮助。我缺少什么?

 private fun openinsiderGetData(): DataFrame<Any> {
        val doc = Jsoup.connect("http://openinsider.com/search?q=TSLA").get()
        val table = doc.select(".tinytable")
        val dataMap =  HashMap<String, ArrayList<String>>()


        table.select("thead").select("tr").forEach {row ->
            row.getElementsByTag("th").forEach { cell ->
                dataMap[cell.text()] = ArrayList<String>()
            }
        }
        // Get the table body cells
        val keys = dataMap.keys.toList()
        Log.d("Keys", keys.toString())

        table.select("tbody").select("tr").forEach {row ->
            var index = 0
            for (i in 0..row.getElementsByTag("td").size) {
                val key = keys[index]
                Log.d("Key", key)
                index += 1
                Log.d("CELL TEXT:", row.getElementsByTag("td")[i].text())
                dataMap[key]?.add(row.getElementsByTag("td")[i].text())
                Log.d("DATA SO FAR", dataMap.toString())
            }
        }
        Log.d("Map:", dataMap.toString())

        val df = emptyDataFrame<Any>()
        for (key in dataMap.keys) {
            val column = columnOf(dataMap[key]) named key
            df.add(column)
        }
        Log.d("Dataframe:", df.toString())
 
        // process the data further
        return df
    }

}

  private fun setupData() {
        lifecycleScope.launch {
            withContext(Dispatchers.Default.limitedParallelism(1)) {
                data = openinsiderGetData()
                val rows = data.values(byRows=true)
                Log.d("ROWS:", rows.toString())
            }
// TODO - Refactor Draw the table

我的数据日志给出:

{Insider Name=[D], Filing Date=[2023-08-08 20:01:17], 1w=[2023-08-04], Ticker=[TSLA], Title=[Taneja Vaibhav], Trade Type=[CFO, CAO], ΔOwn=[S - Sale+OE], 1d=[$261.00], Owned=[-4,000], Price=[104,504], Qty=[-4%], 6m=[-$1,044,000], X=[], Value=[], Trade Date=[], 1m=[]}

这显然是不正常的。我对这种情况如何发生以及如何预防感到困惑。

kotlin
  • 2 个回答
  • 25 Views
Martin Hope
David Frick
Asked: 2023-08-26 05:43:48 +0800 CST

“activity”在 Runnable 中如何工作?我的 Android 表格未显示

  • 6

我正在使用 JSoup 来解析网站(我必须将其设置为使用明文请求,因为不支持 https)。它是为了Runnable避免阻塞主网络线程。但是,当我运行代码时它似乎没有显示。我怀疑这与(因为我在设置它时遇到问题)有关activity,并且我正在使用内部类,但没有返回错误或警告。我究竟做错了什么?

package com.example.investmenthuntermobile


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TableRow
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.investmenthuntermobile.databinding.InsiderFragmentBinding
import org.jsoup.Jsoup


class InsiderFragment : Fragment() {

    private lateinit var binding: InsiderFragmentBinding


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = InsiderFragmentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        setupData()
        //val jsonData = finraGetBondInfo("Boeing")

    }

    private fun setupData() {
        val thread = Thread(Task()).start()
    }


    inner class Task() : Runnable {

        override fun run() {
            val doc = Jsoup.connect("http://openinsider.com/search?q=GME").get()
            val table = doc.select(".tinytable")
            // Get the table header cells
            val tableHeaderRow = TableRow(activity)
            tableHeaderRow.isClickable = true
            table.select("thead").select("tr").forEach {row ->
                row.getElementsByTag("th").forEach { cell ->
                Log.d("CELL text", cell.text())
                val textView = TextView(activity)
                textView.text = cell.text()
                tableHeaderRow.addView(textView)

                }
            }
            // Get the table body cells
            table.select("tbody").select("tr").forEach {row ->
                val tableRow = TableRow(requireActivity())
                tableRow.isClickable = true

                row.getElementsByTag("td").forEach { cell ->
                    val textView = TextView(activity)
                    textView.text = cell.text()
                    tableRow.addView(textView)
                }
            }
        }

    }

}
android
  • 3 个回答
  • 21 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve