r 网状 OOP 方法失败

r reticulate OOP methods failure

我对python比较熟悉,只知道R的基础知识;所以对于需要 "use of R" 的 class,我非常依赖图书馆 "reticulate"。

在过去一两个月里,我已经多次使用它,没有任何问题;然而,今天我定义了一个class。我实例化了 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 世界中有推特 API。我正在处理一个压缩的时间表,我试图避免学习 twitteR,除非那是唯一的选择。如果这真的是个问题,我可以删除 class 架构并毫无问题地调用函数。

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

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


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

> 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 returns 到全局范围。这是因为前一行是空的。与标准 Python REPL 的不同之处在于,后者会抱怨以下函数的缩进不匹配。让我们检查上面定义的 class:

>>> 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

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