gpt4 book ai didi

python - r 网状 OOP 方法失败

转载 作者:行者123 更新时间:2023-12-03 19:12:20 25 4
gpt4 key购买 nike

我对python比较熟悉,只知道R的基础;所以对于需要“使用 R”的类(class),我非常依赖图书馆,“网状”。

在过去一两个月里,我已经多次使用它,没有任何问题;但是,今天我定义了一个类。我毫无问题地实例化了该类,但是当我尝试调用方法时,它返回了错误 AttributeError: 'TweetGrabber' object has no attribute 'user_search'

我会将我的代码分为有效的和无效的,从有效的部分开始:

library('reticulate')

## See the below link to download Python if NOT installed locally.
# https://www.anaconda.com/distribution/

py_config()
use_python(python = '/usr/local/bin/python3')
py_available()
py_install("tweepy")

### === Starts Python environment within R! ===
repl_python()

class TweetGrabber(): # Wrapper for Twitter API.

def __init__(self):
import tweepy
self.tweepy = tweepy
myApi = 'my_key'
sApi = 'my_s_key'
at = 'my_at'
sAt = 'my_s_at'
auth = tweepy.OAuthHandler(myApi, sApi)
auth.set_access_token(at, sAt)
self.api = tweepy.API(auth)


def strip_non_ascii(self,string):
''' Returns the string without non ASCII characters'''
stripped = (c for c in string if 0 < ord(c) < 127)
return ''.join(stripped)

def keyword_search(self,keyword,csv_prefix):
import csv
API_results = self.api.search(q=keyword,rpp=1000,show_user=True)

with open(f'{csv_prefix}.csv', 'w', newline='') as csvfile:
fieldnames = ['tweet_id', 'tweet_text', 'date', 'user_id', 'follower_count',
'retweet_count','user_mentions']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()

for tweet in API_results:
text = self.strip_non_ascii(tweet.text)
date = tweet.created_at.strftime('%m/%d/%Y')
writer.writerow({
'tweet_id': tweet.id_str,
'tweet_text': text,
'date': date,
'user_id': tweet.user.id_str,
'follower_count': tweet.user.followers_count,
'retweet_count': tweet.retweet_count,
'user_mentions':tweet.entities['user_mentions']
})

def user_search(self,user,csv_prefix):
import csv
API_results = self.tweepy.Cursor(self.api.user_timeline,id=user).items()

with open(f'{csv_prefix}.csv', 'w', newline='') as csvfile:
fieldnames = ['tweet_id', 'tweet_text', 'date', 'user_id', 'user_mentions', 'retweet_count']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()

for tweet in API_results:
text = self.strip_non_ascii(tweet.text)
date = tweet.created_at.strftime('%m/%d/%Y')
writer.writerow({
'tweet_id': tweet.id_str,
'tweet_text': text,
'date': date,
'user_id': tweet.user.id_str,
'user_mentions':tweet.entities['user_mentions'],
'retweet_count': tweet.retweet_count
})


t = TweetGrabber() # Instantiates the class we've designed

下一行是触发错误的原因。

t.user_search(user='Telsa',csv_prefix='tesla_tweets') # Find and save to csv Tesla tweets

值得注意的是,我已经在 python 中运行了这段代码,它运行起来非常棒。目标只是一个简单的 API 包装器(用于 tweepy API 包装器),以便我可以使用 1 行代码获取推文并将其存储在 csv 中。

我知道 R 世界中有 twitter API。我正在处理一个压缩的时间表,我试图避免学习 twitteR,除非那是唯一的选择。如果这确实是个问题,我可以删除类架构并毫无问题地调用函数。

我很疑惑为什么 reticulate 可以处理这么多,却达不到执行类方法的目的。我的代码有问题吗?这是否超出了 Reticulate 的范围?

最佳答案

TL;DR:在 REPL 中,空行标记类主体的结尾。以下内容是在全局范围内而不是在类范围内定义的。


似乎 repl_python() 之后的任何内容都直接粘贴到 Reticulate REPL 中(去除多余的缩进)。这里的空行表示类定义的结尾。在您的 __init__ 代码之后是一个空行,因此类定义到此结束。以下函数未在类范围内定义,而是在全局范围内定义。考虑以下示例,我在其中为下面的类粘贴了一些示例代码:

> library('reticulate')
> repl_python()
Python 3.8.1 (/home/a_guest/miniconda3/envs/py38/bin/python3)
Reticulate 1.14 REPL -- A Python interpreter in R.
>>> class Foo:
... def __init__(self):
... self.x = 1
...
>>> def get_x(self):
... return self.x
...
>>>

正如您在 >>> 中看到的那样,在 __init__ 函数的代码之后,REPL 返回到全局范围。这是因为前一行是空的。与标准 Python REPL 的不同之处在于,后者会提示以下函数的缩进不匹配。让我们检查上面定义的类:

>>> Foo.get_x
AttributeError: type object 'Foo' has no attribute 'get_x'
>>> get_x
<function get_x at 0x7fc7fd490430>

显然 get_x 已在全局范围内定义。

解决方案

解决方案是删除空行或通过添加空格使它们非空。例如:

class Foo:
def __init__(self):
self.x = 1
def get_x(self):
return self.x

或使用空格:

class Foo:
def __init__(self):
self.x = 1
# this line contains some spaces
def get_x(self):
return self.x

没有输入空格数,该行必须不为空。

关于python - r 网状 OOP 方法失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60746660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com