prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Default locale to language if no territory given.
  It would be great that if faker was initialized with only a locale and no territory, that it would use  a sensible default.
  
  For example I currently have to do the following if using something such as "en" instead of "en_US".  
  
  ``` py
  from faker import Factory
  from faker import AVAILABLE_LOCALES
  
  locale = 'en'
  if locale not in AVAILABLE_LOCALES:
      locale = next(l for l in AVAILABLE_LOCALES if l.startswith(locale))
  
  factory = Factory.create(locale)
  ```
  
  This happens when using dynamic mock data in local development where django sets the locale to "en" because we do not define territories.
  
  
  </issue>
  <code>
  [start of faker/factory.py]
  1 from __future__ import unicode_literals
  2 from __future__ import absolute_import
  3 import sys
  4 from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
  5 from faker import Generator
  6 from faker import providers as providers_mod
  7 
  8 
  9 class Factory(object):
  10 
  11     @classmethod
  12     def create(cls, locale=None, providers=None, generator=None, **config):
  13 
  14         # fix locale to package name
  15         locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
  16         if '_' in locale:
  17             locale = locale[:2] + locale[2:].upper()
  18         if locale not in AVAILABLE_LOCALES:
  19             raise AttributeError('Invalid configuration for faker locale "{0}"'.format(locale))
  20 
  21         providers = providers or DEFAULT_PROVIDERS
  22 
  23         faker = generator or Generator(**config)
  24         faker.add_provider(providers_mod.BaseProvider)
  25         for provider_name in providers:
  26 
  27             provider_class, lang_found = cls._get_provider_class(provider_name, locale)
  28             provider = provider_class(faker)
  29             provider.__provider__ = provider_name
  30             provider.__lang__ = lang_found
  31             faker.add_provider(provider)
  32 
  33         return faker
  34 
  35     @classmethod
  36     def _get_provider_class(cls, provider, locale=''):
  37 
  38         provider_class = cls._find_provider_class(provider, locale)
  39 
  40         if provider_class:
  41             return provider_class, locale
  42 
  43         if locale and locale != DEFAULT_LOCALE:
  44             # fallback to default locale
  45             provider_class = cls._find_provider_class(provider, DEFAULT_LOCALE)
  46             if provider_class:
  47                 return provider_class, DEFAULT_LOCALE
  48 
  49         # fallback to no locale
  50         provider_class = cls._find_provider_class(provider)
  51         if provider_class:
  52             return provider_class, None
  53 
  54         raise ValueError('Unable to find provider "{0}" with locale "{1}"'.format(provider, locale))
  55 
  56     @classmethod
  57     def _find_provider_class(cls, provider, locale=''):
  58 
  59         path = "{providers}{lang}.{provider}".format(
  60             providers=providers_mod.__package__ or providers_mod.__name__,
  61             lang='.' + locale if locale else '',
  62             provider=provider
  63         )
  64 
  65         try:
  66             __import__(path)
  67         except ImportError:
  68             return None
  69 
  70         return sys.modules[path].Provider
  71
  [end of faker/factory.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/faker/factory.py b/faker/factory.py
  --- a/faker/factory.py
  +++ b/faker/factory.py
  @@ -1,5 +1,6 @@
   from __future__ import unicode_literals
   from __future__ import absolute_import
  +import locale as pylocale
   import sys
   from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES
   from faker import Generator
  @@ -13,8 +14,7 @@
   
           # fix locale to package name
           locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE
  -        if '_' in locale:
  -            locale = locale[:2] + locale[2:].upper()
  +        locale = pylocale.normalize(locale).split('.')[0]
           if locale not in AVAILABLE_LOCALES:
               raise AttributeError('Invalid configuration for faker locale "{0}"'.format(locale))
  
verification_info: {"golden_diff": "diff --git a/faker/factory.py b/faker/factory.py\n--- a/faker/factory.py\n+++ b/faker/factory.py\n@@ -1,5 +1,6 @@\n from __future__ import unicode_literals\n from __future__ import absolute_import\n+import locale as pylocale\n import sys\n from faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES\n from faker import Generator\n@@ -13,8 +14,7 @@\n \n         # fix locale to package name\n         locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE\n-        if '_' in locale:\n-            locale = locale[:2] + locale[2:].upper()\n+        locale = pylocale.normalize(locale).split('.')[0]\n         if locale not in AVAILABLE_LOCALES:\n             raise AttributeError('Invalid configuration for faker locale \"{0}\"'.format(locale))\n", "issue": "Default locale to language if no territory given.\nIt would be great that if faker was initialized with only a locale and no territory, that it would use  a sensible default.\n\nFor example I currently have to do the following if using something such as \"en\" instead of \"en_US\".  \n\n``` py\nfrom faker import Factory\nfrom faker import AVAILABLE_LOCALES\n\nlocale = 'en'\nif locale not in AVAILABLE_LOCALES:\n    locale = next(l for l in AVAILABLE_LOCALES if l.startswith(locale))\n\nfactory = Factory.create(locale)\n```\n\nThis happens when using dynamic mock data in local development where django sets the locale to \"en\" because we do not define territories.\n\n", "before_files": [{"content": "from __future__ import unicode_literals\nfrom __future__ import absolute_import\nimport sys\nfrom faker import DEFAULT_LOCALE, DEFAULT_PROVIDERS, AVAILABLE_LOCALES\nfrom faker import Generator\nfrom faker import providers as providers_mod\n\n\nclass Factory(object):\n\n    @classmethod\n    def create(cls, locale=None, providers=None, generator=None, **config):\n\n        # fix locale to package name\n        locale = locale.replace('-', '_') if locale else DEFAULT_LOCALE\n        if '_' in locale:\n            locale = locale[:2] + locale[2:].upper()\n        if locale not in AVAILABLE_LOCALES:\n            raise AttributeError('Invalid configuration for faker locale \"{0}\"'.format(locale))\n\n        providers = providers or DEFAULT_PROVIDERS\n\n        faker = generator or Generator(**config)\n        faker.add_provider(providers_mod.BaseProvider)\n        for provider_name in providers:\n\n            provider_class, lang_found = cls._get_provider_class(provider_name, locale)\n            provider = provider_class(faker)\n            provider.__provider__ = provider_name\n            provider.__lang__ = lang_found\n            faker.add_provider(provider)\n\n        return faker\n\n    @classmethod\n    def _get_provider_class(cls, provider, locale=''):\n\n        provider_class = cls._find_provider_class(provider, locale)\n\n        if provider_class:\n            return provider_class, locale\n\n        if locale and locale != DEFAULT_LOCALE:\n            # fallback to default locale\n            provider_class = cls._find_provider_class(provider, DEFAULT_LOCALE)\n            if provider_class:\n                return provider_class, DEFAULT_LOCALE\n\n        # fallback to no locale\n        provider_class = cls._find_provider_class(provider)\n        if provider_class:\n            return provider_class, None\n\n        raise ValueError('Unable to find provider \"{0}\" with locale \"{1}\"'.format(provider, locale))\n\n    @classmethod\n    def _find_provider_class(cls, provider, locale=''):\n\n        path = \"{providers}{lang}.{provider}\".format(\n            providers=providers_mod.__package__ or providers_mod.__name__,\n            lang='.' + locale if locale else '',\n            provider=provider\n        )\n\n        try:\n            __import__(path)\n        except ImportError:\n            return None\n\n        return sys.modules[path].Provider\n", "path": "faker/factory.py"}]}