r网状OOP方法失败

2024-09-28 23:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我非常熟悉python,只知道R的基本知识;所以对于一个需要“使用R”的类,我非常依赖于库“网状”

在过去的一两个月里,我已经多次使用它,没有任何问题;然而,今天我定义了一个类。我没有问题地实例化了这个类,但是当我试图调用一个方法时,它返回了错误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包装器),这样我就可以用一行代码在csv中抓取和存储推文

我知道在R世界中有twitter API。我在一个压缩的时间轴上工作,我试图避免学习twitteR,除非这是唯一的选择。如果这真的是一个问题,我可以删除类体系结构并调用函数而不会出现问题

我很纳闷为什么Networkite可以处理这么多,却不能执行类方法。我的代码中有问题吗?这是否超出了网状结构的范围


Tags: csvtextselfapiiddatecountat
1条回答
网友
1楼 · 发布于 2024-09-28 23:19:52

TL;DR:在REPL中,空行表示类正文的结尾。下面的内容是在全局范围中定义的,而不是在类范围中定义的


似乎repl_python()后面的任何内容都直接粘贴到网状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

不导入空格数,行必须不为空

相关问题 更多 >