io.BufferedReader是否真正支持readline?

时间:2018-07-22 19:48:20

标签: python io python-3.6

根据documentation IFunction继承public class EmailValidatorBehavior : Behavior<Entry> { const string digitRegex = @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$"; static readonly BindablePropertyKey IsValidPropertyKey = BindableProperty.CreateReadOnly("IsValid", typeof(bool), typeof(EmailValidatorBehavior), false); public static readonly BindableProperty IsValidProperty = IsValidPropertyKey.BindableProperty; static readonly BindablePropertyKey IsVisiblePropertyKey = BindableProperty.CreateReadOnly("IsVisible", typeof(bool), typeof(EmailValidatorBehavior), false); public static readonly BindableProperty IsVisibleProperty = IsVisiblePropertyKey.BindableProperty; public bool IsValid { get { return (bool)base.GetValue(IsValidProperty); } private set { base.SetValue(IsValidPropertyKey, value); } } public bool IsVisible { get { return (bool)base.GetValue(IsVisibleProperty); } private set { base.SetValue(IsVisiblePropertyKey, value); } } protected override void OnAttachedTo(Entry entry) { entry.TextChanged += OnEntryTextChanged; base.OnAttachedTo(entry); } protected override void OnDetachingFrom(Entry entry) { entry.TextChanged -= OnEntryTextChanged; base.OnDetachingFrom(entry); } void OnEntryTextChanged(object sender, TextChangedEventArgs e) { if (e.NewTextValue.Length > 0) { IsVisible = true; Entry entry = (Entry)sender; IsValid = Regex.IsMatch(e.NewTextValue, digitRegex); if (IsValid) // Check only if we have a valid email { // Here we validate if the email contains our requirements String email = entry.Text; int pos = email.IndexOf("@"); // Exclude the domain string username = email.Substring(0, pos); if (username.Contains(FRIEND) || username.Contains(FRIENDS)) { IsValid = true; } else IsValid = false; } } else IsVisible = false; } public class BooleanToObjectConverter<Image> : IValueConverter { public Image FalseObject { set; get; } public Image TrueObject { set; get; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? this.TrueObject : this.FalseObject; } //public object Convert(object value, Type targetType, object parameter, CultureInfo culture) //{ // throw new NotImplementedException(); //} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return ((Image)value).Equals(this.TrueObject); } //public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) //{ // throw new NotImplementedException(); //} } <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="CoTraveller.RegistrationPage" xmlns:local="clr-namespace:CoTraveller"> <Application.Resources> <!-- Application resource dictionary --> <ResourceDictionary> <local:BooleanToObjectConverter x:Key="boolToStyleImage" x:TypeArguments="Style"> <local:BooleanToObjectConverter.FalseObject> <Style TargetType="Image"> <Setter Property="HeightRequest" Value="24" /> <Setter Property="Source" Value="your_wrong_image_here.png" /> </Style> </local:BooleanToObjectConverter.FalseObject> <x:BooleanToObjectConverter.TrueObject> <Style TargetType="Image"> <Setter Property="HeightRequest" Value="24" /> <Setter Property="Source" Value="your_correct_image_here.png" /> </Style> </x:BooleanToObjectConverter.TrueObject> </local:BooleanToObjectConverter> </ResourceDictionary> </Application.Resources> </Application> -> io.BufferedReader-> io.IOBase。因此,应该实现io.IOBaseio.BufferedIOBase,但事实并非如此,因为我得到了io.BufferedReader。一个最小的例子:

数据样本:

readline

测试代码:

readinto

例外:

AttributeError

我想念什么吗?

更新

有趣的是,您可以从$ printf 'foo bar\nspam ham\n' | gzip -c > compressed_file.gz 中手动获取一个import gzip import io with io.BufferedReader(gzip.open('compressed_file.gz', 'rt')) as buffer: buffer.readline() 来包装字节流,并且--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-2-215a0d9b4feb> in <module>() 3 4 with io.BufferedReader(gzip.open('compressed_file.gz', 'rt')) as buffer: ----> 5 buffer.readline() AttributeError: '_io.TextIOWrapper' object has no attribute 'readinto' 方法将起作用。

io.TextIOWrapper

似乎io.BufferedReader返回的readline实例与import gzip import io with io.TextIOWrapper(io.BufferedReader(gzip.open('compressed_file.gz'))) as buffer: buffer.readline() 实例不同,尽管后者是is directly imported from _io

1 个答案:

答案 0 :(得分:0)

是的,您的确确实缺少关键细节:BufferedReader包装了一个现有的RawIOBase对象:

  

io.BufferedReader类(原始,buffer_size = DEFAULT_BUFFER_SIZE)

     

提供对可读的顺序RawIOBase对象的更高级别访问的缓冲区。

并且因为您已经以 text 模式打开了gzip文件,所以它不是RawIOBase实例:

>>> f = gzip.open('compressed_file.gz', 'rt')
>>> isinstance(f, io.RawIOBase)
False

换句话说,这是用户错误。

如果将BufferedReader包裹在 binary 文件中,则会发现readline方法可以正常工作:

>>> io.BufferedReader(io.BytesIO(b'foo\nbar')).readline()
b'foo\n'